package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.service.FeeCompanyService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.companyorder.pojo.dto.CompanyOrderListDTO;
import com.xbongbong.pro.companyorder.pojo.vo.CompanyOrderListVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @Author shian
 * @create 2020-01-14 14:18
 * @Description 企业订单
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/companyOrder")
public class CompanyOrderController {

    @Resource
    private FeeCompanyService feeCompanyService;

    /**
     * 加载企业中所有的套餐订单
     * @param companyOrderListDTO
     * @param br
     * @return java.lang.String
     * @author 石安
     * @date 2020-01-14
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid CompanyOrderListDTO companyOrderListDTO, BindingResult br) {
        XbbResponse<CompanyOrderListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CompanyOrderListVO listVO = feeCompanyService.list(companyOrderListDTO);
                response = new XbbResponse<>(listVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
